package com.ynet.hmds28.service.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.taobao.pandora.boot.loader.util.StringUtils;
import com.ynet.hm.user.domain.UserBean;
import com.ynet.hm.user.service.OpenPlatformService;
import com.ynet.hm.user.service.PushService;
import com.ynet.hm.user.service.QueryCanSubService;
import com.ynet.hm.user.service.QueryConsumeByUserIdService;
import com.ynet.hm.user.service.QueryCoverSubServiceService;
import com.ynet.hm.user.service.QueryProvideByUserIdService;
import com.ynet.hm.user.service.QueryServiceCallFlowService;
import com.ynet.hm.user.service.RealTimeControlService;
import com.ynet.hm.user.service.ServiceRealTimeControlService;
import com.ynet.hm.user.service.SubscribeService;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hm.utils.enums.ExceptionEnum;
import com.ynet.hm.utils.exception.MyException;
import com.ynet.hmds28.service.redis.RedisService;

@RestController
public class OpenPlatformController {
	@Autowired
	private  OpenPlatformService  openPlatformServcie;
	@Autowired
	private  QueryCanSubService queryCanSubService;
	@Autowired
	private  QueryConsumeByUserIdService queryConsumeByUserIdService;
	@Autowired
	private  QueryCoverSubServiceService queryCoverSubServiceService;
	@Autowired
	private QueryProvideByUserIdService queryProvideByUserIdService;
	@Autowired
	private QueryServiceCallFlowService queryServiceCallFlowService;
	@Autowired
	private SubscribeService subscribeService;
	@Autowired
	private PushService pushService;
	@Autowired
	private RedisService redisService;
	@Autowired
	private RealTimeControlService realTimeControlService;
	@Autowired
	private ServiceRealTimeControlService serviceRealTimeControlService;
	// 平台公钥
	@Value("${platformPublicKey}")
	public String platformPublicKey;
	/**
	 * 开放平台登录
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/openPlatformLogin", method = RequestMethod.POST)
	public Result<?> openPlatformLogin(HttpServletRequest request){
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		if(StringUtils.isEmpty(userName)){
    		throw new MyException(ExceptionEnum.USERNAME_IS_NULL);
    	}
    	if(StringUtils.isEmpty(password)){
    		throw new MyException(ExceptionEnum.PASSWORD_IS_NULL);
    	}
    	HttpSession session = request.getSession();
    	UserBean user =openPlatformServcie.openPlatformLogin(userName,password);
    	session.setAttribute("userInfo", user);
		return ResultUtil.success(user);
	}
	/**
	 * 开放平台商户注册
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/openPlatformRegister", method = RequestMethod.POST)
	public Result<?> openPlatformRegister(HttpServletRequest request){
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		if(StringUtils.isEmpty(userName)){
    		throw new MyException(ExceptionEnum.USERNAME_IS_NULL);
    	}
    	if(StringUtils.isEmpty(password)){
    		throw new MyException(ExceptionEnum.PASSWORD_IS_NULL);
    	}
    	//平台公钥存入redis
    	redisService.set("platFormPublicKey", platformPublicKey);
		return openPlatformServcie.openPlatformRegister(userName, password, platformPublicKey);
	}
	/**
	 * 查询商户的订阅服务列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/queryConsumeByUserId", method = RequestMethod.POST)
	public Result<?> queryConsumeByUserId(HttpServletRequest request){
		HttpSession session = request.getSession();
		UserBean user=(UserBean)session.getAttribute("userInfo");
		String userId=user.getUserid();
		if(StringUtils.isEmpty(userId)){
			throw new MyException(ExceptionEnum.GETUSER_ERROR);
		}
		String pageNo=request.getParameter("pageNo");
		String pageSize=request.getParameter("pageSize");
		return queryConsumeByUserIdService.queryConsumeByUserId(userId,pageNo,pageSize);
	}
	/**
	 * 查询服务调用流水明细
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/queryServiceCallFlow", method = RequestMethod.POST)
	public Result<?> queryServiceCallFlow(HttpServletRequest request){
		String serviceConsumeId=request.getParameter("serviceConsumeId");
		String pageNo=request.getParameter("pageNo");
		String pageSize=request.getParameter("pageSize");
		if(StringUtils.isEmpty(serviceConsumeId)){
			throw new MyException(ExceptionEnum.SERVICE_IS_NULL);
		}
		return queryServiceCallFlowService.queryServiceCallFlow(serviceConsumeId,pageNo,pageSize);
	}
	/**
	 * 查询可订阅服务列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/queryCanSubService", method = RequestMethod.POST)
	public Result<?> queryCanSubService(HttpServletRequest request){
		HttpSession session = request.getSession();
		UserBean user=(UserBean)session.getAttribute("userInfo");
		String userId=user.getUserid();
		if(StringUtils.isEmpty(userId)){
			throw new MyException(ExceptionEnum.GETUSER_ERROR);
		}
		String pageNo=request.getParameter("pageNo");
		String pageSize=request.getParameter("pageSize");
		return queryCanSubService.queryCanSubService(userId,pageNo,pageSize);
	}
	/**
	 * 查询商户的发布服务列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/queryProvideByUserId", method = RequestMethod.POST)
	public Result<?> queryProviceByUserId(HttpServletRequest request){
		HttpSession session = request.getSession();
		UserBean user=(UserBean)session.getAttribute("userInfo");
		String userId=user.getUserid();
		if(StringUtils.isEmpty(userId)){
			throw new MyException(ExceptionEnum.GETUSER_ERROR);
		}
		String pageNo=request.getParameter("pageNo");
		String pageSize=request.getParameter("pageSize");
		return queryProvideByUserIdService.queryProvideByUserId(userId,pageNo,pageSize);
	}
	/**
	 * 查询服务被订阅列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/queryCoverSubService", method = RequestMethod.POST)
	public Result<?> queryCoverSubService(HttpServletRequest request){
		String serviceProvideId=request.getParameter("serviceProvideId");
		if(StringUtils.isEmpty(serviceProvideId)){
			throw new MyException(ExceptionEnum.SERVICE_IS_NULL);
		}
		String pageNo=request.getParameter("pageNo");
		String pageSize=request.getParameter("pageSize");
		return queryCoverSubServiceService.queryCoverSubService(serviceProvideId,pageNo,pageSize);
	}
	/**
	 * 订阅服务
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/subscribeService", method = RequestMethod.POST)
	public Result<?> subscribeService(HttpServletRequest request){
		HttpSession session = request.getSession();
		UserBean user=(UserBean)session.getAttribute("userInfo");
		String userId=user.getUserid();
		String serviceId=request.getParameter("serviceId");
		return subscribeService.SubscribeService(userId, serviceId);
	}
	/**
	 * 发布服务
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/pushService", method = RequestMethod.POST)
	public Result<?> pushService(HttpServletRequest request){
		HttpSession session = request.getSession();
		UserBean user=(UserBean)session.getAttribute("userInfo");
		String userId=user.getUserid();
		String serviceName=request.getParameter("serviceName");
		String serviceUrl=request.getParameter("serviceUrl");
		String serviceMethod=request.getParameter("serviceMethod");
		String serviceCostRule=request.getParameter("serviceCostRule");
		String flowControl=request.getParameter("flowControl");
		String importField=request.getParameter("importField");
		String exportField=request.getParameter("exportField");
		String serivceDesc=request.getParameter("serviceDesc");
		Result<?> result=pushService.pushService(userId, serviceName, serviceUrl, serviceMethod, serviceCostRule, flowControl, importField, exportField, serivceDesc);
		String proId=(String) result.getData();
		//流量控制加入缓存中
		redisService.set(proId, flowControl);
		return result;
	}
	/**
	 * 更改服务流量控制
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/updateFlowControl", method = RequestMethod.POST)
	public Result<?> updateFlowControl(HttpServletRequest request){
		String serviceProvideId=request.getParameter("serviceProviceId");
		String flowControl=request.getParameter("flowControl");
		if(StringUtils.isEmpty(flowControl)){
			throw new MyException(ExceptionEnum.SERVICEFLOWCONTROL_IS_NULL);
		}
		redisService.set(serviceProvideId, flowControl);
		return ResultUtil.success();
	}
	/**
	 * 订阅列表实时监控
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/realTimeControl", method = RequestMethod.POST)
	public Result<?> realTimeControl(HttpServletRequest request){
		String serviceConsumeId=request.getParameter("serviceConsumeId");
		return realTimeControlService.realTimeControl(serviceConsumeId);
	}
	/**
	 * 服务列表实时监控
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/api/user/serviceRealTimeControl", method = RequestMethod.POST)
	public Result<?> serviceRealTimeControl(HttpServletRequest request){
		String serviceProvideId=request.getParameter("serviceProvideId");
		return serviceRealTimeControlService.serviceRealTimeControl(serviceProvideId);
	}
}
